﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

   /// <summary>
   /// Stream类的扩展方法类
   /// </summary>
  public static  class StreamExtensions
    {
        /// <summary>
        /// 返回流的字符串形式
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string ReadAllAsString(this Stream stream)
        {
            using (var sr = new StreamReader(stream))
            {
                return sr.ReadToEnd();
            }
        }

      /// <summary>
      /// 将流写入字节数组
      /// </summary>
      /// <param name="stream"></param>
      /// <returns></returns>
        public static byte[] ReadAllAsBytes(this Stream stream)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }
    }
